﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace physio.assist
{
    /// <summary>
    /// Interaction logic for Pescoco.xaml
    /// </summary>
    public partial class Pescoco : Window
    {
        public Pescoco()
        {
            InitializeComponent();



            //Ajusta posição e tamanho das janelas
            double width = System.Windows.SystemParameters.PrimaryScreenWidth;
            double height = System.Windows.SystemParameters.PrimaryScreenHeight;

            winPescoco.Width = width;
            winPescoco.Height = height;

            winPescoco.Left = 0;
            winPescoco.Top = 0;



            //Ajusta posicionamento e tamanho dos panels
            pnlGeral.Width = width;
            pnlGeral.Height = height;

            pnlVoltar.Height = height;
            pnlPrimaria.Height = height;
            pnlSecundaria.Height = height;

            pnlVoltar.Width = width * 0.2;
            pnlPrimaria.Width = width * 0.5;
            pnlSecundaria.Width = width * 0.3;





            //Carrega desempenho
            CarregaInformacoeJunta(1);//Hardcode

            CoresDosLabels(lblPescoco30PorcentagemAngulo);
            CoresDosLabels(lblPescoco30PorcentagemTempo);



        }








        //DESEMPENHO
        private void CarregaInformacoeJunta(int paciente)
        {

            //Obtem valor comparativo do histórico geral do paciente com seu desempenho na ultima semana
            PhysioAssistWS.PhysioAssistSoapClient ws = new PhysioAssistWS.PhysioAssistSoapClient();


            double valorAnguloMensal = ws.Compara(DateTime.Now.AddYears(-1000), DateTime.Now.AddDays(-30 - 1), DateTime.Now.AddDays(-30), DateTime.Now, "angulo", "media", "Pescoço", paciente);
            double valorTempoMensal = ws.Compara(DateTime.Now.AddYears(-1000), DateTime.Now.AddDays(-30 - 1), DateTime.Now.AddDays(-30), DateTime.Now, "tempo", "media", "Pescoço", paciente);
            double valorAnguloGeral = ws.Calcula(DateTime.Now.AddYears(-1000), DateTime.Now, "angulo", "media", paciente, "Pescoço");
            double valorTempoGeral = ws.Calcula(DateTime.Now.AddYears(-1000), DateTime.Now, "tempo", "media", paciente, "Pescoço");


            TextBlock txtMesAngulo = new TextBlock();
            TextBlock txtMesTempo = new TextBlock();
            TextBlock txtGeralAngulo = new TextBlock();
            TextBlock txtGeralTempo = new TextBlock();


            lblPescoco30PorcentagemAngulo.Content = valorAnguloMensal + "%";
            txtMesAngulo.Text = "De variação de amplitude do movimento do pescoço no último mês";
            txtMesAngulo.TextWrapping = TextWrapping.Wrap;
            lblPescoco30MensagemAngulo.Content = txtMesAngulo;


            lblPescoco30PorcentagemTempo.Content = valorTempoMensal + "%";
            txtMesTempo.Text = "De variação de tempo do movimento do pescoço no último mês\n\n\n";
            txtMesTempo.TextWrapping = TextWrapping.Wrap;
            lblPescoco30MensagemTempo.Content = txtMesTempo;


            lblPescocoGeralPorcentagemAngulo.Content = valorAnguloGeral + "º";
            txtGeralAngulo.Text = "É a média geral de amplitude do movimento do pescoço do paciente";
            txtGeralAngulo.TextWrapping = TextWrapping.Wrap;
            lblPescocoGeralMensagemAngulo.Content = txtGeralAngulo;


            lblPescocoGeralPorcentagemTempo.Content = Math.Round(valorTempoGeral, 1) + " segundos";
            txtGeralTempo.Text = "É a média geral de amplitude do movimento do pescoço do paciente";
            txtGeralTempo.TextWrapping = TextWrapping.Wrap;
            lblPescocoGeralMensagemTempo.Content = txtGeralTempo;




        }
        private void CoresDosLabels(Label lbl)
        {

            try
            {

                double valor = double.Parse(lbl.Content.ToString().Replace("%", ""));

                if (valor > 0) lbl.Foreground = Brushes.DodgerBlue;
                if (valor == 0) lbl.Foreground = Brushes.Black;
                if (valor < 0) lbl.Foreground = Brushes.Firebrick;


            }
            catch { }

        }







        //RECONHECIMENTO DE VOZ
        private Microsoft.Speech.Recognition.SpeechRecognitionEngine speechEngine;
        private Microsoft.Kinect.KinectSensor sensor;
        private void winAgenda_Loaded(object sender, RoutedEventArgs e)
        {


            //Obtem o sensor ativo
            foreach (var potentialSensor in Microsoft.Kinect.KinectSensor.KinectSensors)
            {
                if (potentialSensor.Status == Microsoft.Kinect.KinectStatus.Connected)
                {
                    sensor = potentialSensor;
                    break;
                }
            }


            if (null != sensor)
            {

                try
                {
                    sensor.Start();
                }
                catch (System.IO.IOException)
                {
                    sensor = null;
                }
            }








            //RECONHECIMENTO DE VOZ
            Microsoft.Speech.Recognition.RecognizerInfo ri = GetKinectRecognizer();

            if (null != ri)
            {

                this.speechEngine = new Microsoft.Speech.Recognition.SpeechRecognitionEngine(ri.Id);

                // Create a grammar from grammar definition XML file.
                using (var memoryStream = new System.IO.MemoryStream(Encoding.ASCII.GetBytes(Properties.Resources.PhysioAssistGrammar)))
                {
                    var g = new Microsoft.Speech.Recognition.Grammar(memoryStream);
                    speechEngine.LoadGrammar(g);
                }

                speechEngine.SpeechRecognized += SpeechRecognized;

                speechEngine.SetInputToAudioStream(sensor.AudioSource.Start(), new Microsoft.Speech.AudioFormat.SpeechAudioFormatInfo(Microsoft.Speech.AudioFormat.EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
                speechEngine.RecognizeAsync(Microsoft.Speech.Recognition.RecognizeMode.Multiple);
            }


        }
        private static Microsoft.Speech.Recognition.RecognizerInfo GetKinectRecognizer()
        {
            foreach (Microsoft.Speech.Recognition.RecognizerInfo recognizer in Microsoft.Speech.Recognition.SpeechRecognitionEngine.InstalledRecognizers())
            {
                string value;
                recognizer.AdditionalInfo.TryGetValue("Kinect", out value);
                if ("True".Equals(value, StringComparison.OrdinalIgnoreCase) && "en-US".Equals(recognizer.Culture.Name, StringComparison.OrdinalIgnoreCase))
                {
                    return recognizer;
                }
            }

            return null;
        }
        private void SpeechRecognized(object sender, Microsoft.Speech.Recognition.SpeechRecognizedEventArgs e)
        {
            // Speech utterance confidence below which we treat speech as if it hadn't been heard
            const double ConfidenceThreshold = 0.3;

            if (e.Result.Confidence >= ConfidenceThreshold)
            {
                switch (e.Result.Semantics.Value.ToString())
                {
                    case "CLOSE":
                        winPescoco.Close();
                        break;

                    case "FECHAR":
                        winPescoco.Close();
                        break;
                }
            }
        }











    }
}
